package org.create.builder;

import org.create.bean.Dependency;
import org.create.bean.DependencyConfig;
import org.create.utils.IoTool;
import org.create.utils.LogTool;

import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;

import static org.create.bean.Constants.*;


public class BuildProject {
    private static BufferedWriter bw;

    private static void buildProjectDir() throws IOException {
        File dir = new File(PROJECT_ROOT);
        if (!dir.exists()) {
            throw new RuntimeException("根目录不存在");
        }
        dir = new File(dir, PROJECT_NAME);
        boolean r = dir.mkdir();
        LogTool.printLog(r, "新建项目", dir);

        SAVE_TABLE_INFO_PATH = new File(dir, "src/main/resources/data.json").getPath();

        File pom = new File(dir, "pom" + XML_SUFFIX);
        try {
            r = pom.createNewFile();
            LogTool.printLog(r, "新建文件", pom);
        } catch (IOException e) {
            LogTool.printLog("文件创建失败", pom);
        }

        File[] files = {
                new File(dir, "src/main/java"),
                new File(dir, "src/main/resources"),
                new File(dir, "src/test/java"),
        };
        for (File file : files) {
            r = file.mkdirs();
            LogTool.printLog(r, "新建目录", file);
        }
    }

    private static void buildPom() throws IOException {
        bw.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
        appendLine();
        bw.write("<project xmlns=\"http://maven.apache.org/POM/4.0.0\"");
        appendLine();
        bw.write("         xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"");
        appendLine();
        bw.write("         xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd\">");
        appendLine();
        bw.write("    <modelVersion>4.0.0</modelVersion>");
        appendLine(2);
        bw.write("    <parent>");
        appendLine();
        bw.write("        <groupId>org.springframework.boot</groupId>");
        appendLine();
        bw.write("        <artifactId>spring-boot-starter-parent</artifactId>");
        appendLine();
        bw.write("        <version>" + DependencyConfig.springVersion + "</version>");
        appendLine();
        bw.write("    </parent>");
        appendLine(2);
        bw.write("    <groupId>" + PACKAGE_BASE + "</groupId>");
        appendLine();
        bw.write("    <artifactId>" + PROJECT_NAME + "</artifactId>");
        appendLine();
        bw.write("    <version>1.0-SNAPSHOT</version>");
        appendLine(2);
        bw.write("    <properties>");
        appendLine();
        for (String version : DependencyConfig.getProperties()) {
            bw.write("        " + version);
            appendLine();
        }
        bw.write("    </properties>");
        appendLine();
        buildDependencies();
        bw.write("</project>");
        appendLine();

    }

    private static void buildDependencies() throws IOException {
        bw.write("    <dependencies>");
        appendLine();
        buildDependency();
        bw.write("    </dependencies>");
        appendLine();
    }

    private static void buildDependency() throws IOException {
        for (Dependency dependency : DependencyConfig.getDependencyList()) {
            bw.write("        <dependency>");
            appendLine();
            bw.write("            <groupId>" + dependency.groupId + "</groupId>");
            appendLine();
            bw.write("            <artifactId>" + dependency.artifactId + "</artifactId>");
            appendLine();
            if (!dependency.version.isEmpty()) {
                bw.write("            <version>" + dependency.version + "</version>");
                appendLine();
            }
            if (!dependency.scope.isEmpty()) {
                bw.write("            <scope>" + dependency.scope + "</scope>");
                appendLine();
            }
            bw.write("        </dependency>");
            appendLine();
        }
    }

    private static void appendLine() throws IOException {
        appendLine(1);
    }

    private static void appendLine(int loop) throws IOException {
        if (loop == 1) {
            bw.newLine();
        } else {
            for (int i = 0; i < loop; i++) {
                bw.newLine();
            }
        }
    }

    public static void run() {
        try {
            buildProjectDir();
            String project_root = PROJECT_ROOT;
            if (project_root.lastIndexOf("/") + 1 != project_root.length()) {
                project_root += "/";
            }
            File file = new File(project_root + PROJECT_NAME + "/" + "pom" + XML_SUFFIX);
            if (file.exists()) {
                try {
                    bw = IoTool.createBw(file);
                    buildPom();
                    bw.flush();
                } catch (Exception e) {
                    LogTool.printError(e);
                } finally {
                    IoTool.close();
                }
            } else {
                LogTool.printLog("文件不存在", file);
                throw new RuntimeException("文件不存在");
            }
        } catch (IOException e) {
            LogTool.printError(e);
        }
    }
}
